package com.client.koshercook.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;

/** a wrapper for a PushButton used for displaying and changing rating of a Recipes */
public class Rater extends Composite{

	private VerticalPanel panel = new VerticalPanel();
	private HorizontalPanel starsPanel = new HorizontalPanel();
	private Label text = new Label("לחץ כדי לדרג");
	private String link = null;
	private Image stars[] = new Image[5];
	private PushButton buttons[] = new PushButton[5];
		
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	
	/** create a new Rater. use setRecipe to associate it with a recipe */
	public Rater() {
		
		text.setStyleName("generalInfo");
		
		for(int i=0; i<5; i++){
			stars[i] = new Image("star_full.gif");
			buttons[i] = new PushButton(stars[i]);
			buttons[i].setEnabled(false);
			starsPanel.add(buttons[i]);
		}
		buttons[0].addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(link==null)
					return;
				disable();
				getNewGrade(link, 1);			//update server grade, and update display
			}
		});
		buttons[0].setTitle("כוכב אחד");
		buttons[1].addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(link==null)
					return;
				disable();
				getNewGrade(link, 2);
			}
		});
		buttons[2].addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(link==null)
					return;
				disable();
				getNewGrade(link, 3);
			}
		});
		buttons[3].addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(link==null)
					return;
				disable();
				getNewGrade(link, 4);
			}
		});
		buttons[4].addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(link==null)
					return;
				disable();
				getNewGrade(link, 5);
			}
		});
		buttons[4].setTitle("חמישה כוכבים");

		panel.add(starsPanel);
		panel.add(text);
		initWidget(panel);
	}
	
	/** associate the rater with a recipe, and show it's current grade */
	public void setRecipe(String link, int initialGrade){
		this.link = link;
		updateGradeDisplay(initialGrade);
		for(int i=0; i<5; i++){
			buttons[i].setEnabled(true);
		}
	}
	
	/** get the new Grade of recipe recipeName from the server */
	private void getNewGrade(String recipeName, int newGrade) {
		greetingService.getNewGrade(recipeName, newGrade, new AsyncCallback<Integer>(){
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(Integer result) {
				updateGradeDisplay(result);	//setting the new image for the rater
				if(result>0)
					Client.model.setInfoTextAndPO("דירוג מתכון נקלט בהצלחה");
				else if(result==0)
					Client.model.setInfoTextAndPO("עליך להיות מחובר כדי לדרג מתכון");
				else if(result<0)
					Client.model.setInfoTextAndPO("כבר דירגת מתכון זה בעבר");
			}
		});
	}
	
	/** set the display according to the average ranking */
	private void updateGradeDisplay(int grade){
		if(grade<=0)//grade error - keep the old grade
			return;
		for(int i=0; i<5; i++){
			if(i<grade)
				stars[i].setUrl("star_full.gif");
			else
				stars[i].setUrl("star_empty.gif");
		}
		return;
	}
	
	public void disable(){
		for(int j=0; j<5; j++){
			buttons[j].setEnabled(false);	//disabling the button from further rating
		}
		return;
	}
}